package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.EventCenterOutput;
import com.sanpu.kxe.business.main.contract.EventCenterContract;
import com.sanpu.kxe.business.main.presenter.EventCenterPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundedImageView;
import com.sanpu.kxe.rxservice.RequestMap;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Description：活动中心
 * Created by 薛勇军 on 2018/7/9.
 */

public class EventCenterActivity extends BaseTitleActivity implements EventCenterContract.View {

    @Bind(R.id.recycler_view)
    RecyclerView vRecyclerview;
    @Bind(R.id.emptyView)
    View emptyView;

    private EventCenterContract.Presenter mPresenter;

    private EventCenterAdapter mCenterAdapter;

    private List<EventCenterOutput> datas = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_event_center;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("活动中心");

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext,
                LinearLayoutManager.VERTICAL, false);
        vRecyclerview.setLayoutManager(layoutManager);

        mCenterAdapter = new EventCenterAdapter();
        vRecyclerview.setAdapter(mCenterAdapter);
        mPresenter = new EventCenterPresenter(this);
        mPresenter.getActInfList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        mCenterAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position < 0 || position >= datas.size()) return;
                EventCenterOutput eventInfo = datas.get(position);
                if (eventInfo != null && eventInfo.isEnbFlg()) {
                    String url = UrlConstants.getBaseUrl() + eventInfo.getPicController();
                    url += "?" + new RequestMap().map2ReqStr();
                    MainOpenProvider.getInstance().openBrowserX5Activity(mContext, eventInfo.getActNm(), url);
                }
            }
        });
    }

    @Override
    public void showViewData(List<EventCenterOutput> outputs) {
        if (outputs != null && outputs.size() > 0) {
            vRecyclerview.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            datas.clear();
            datas.addAll(outputs);
            mCenterAdapter.setNewData(datas);
        } else {
            vRecyclerview.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    public class EventCenterAdapter extends BaseQuickAdapter<EventCenterOutput, BaseViewHolder> {

        public EventCenterAdapter() {
            super(R.layout.item_event_center, datas);
        }

        @Override
        protected void convert(BaseViewHolder helper, EventCenterOutput item) {
            JKImageLoader.getInstance().displayImage(mContext, UrlConstants.getBaseUrl() + item.getPicAdr(),
                    (RoundedImageView) helper.getView(R.id.image));
            helper.setVisible(R.id.overLayout, !item.isEnbFlg());
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(EventCenterContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
